/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Aug 16, 2010, 9:10:58 AM
 */
package com.ivanbiz.gui;

import com.birosoft.liquid.LiquidLookAndFeel;
import com.ivanbiz.dao.LockDateDAO;
import com.ivanbiz.dao.UserDAO;
import com.ivanbiz.impl.LockDateDAOImpl;
import com.ivanbiz.impl.UserDAOImpl;
import com.ivanbiz.model.User;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Aprisma
 */
public class Login extends javax.swing.JFrame {

    UserDAO userDAO = new UserDAOImpl();

    /** Creates new form Login */
    public Login() {
        try {
            initComponents();
            updateData();
            LockDateDAO dao = new LockDateDAOImpl();
            dao.lockAndCheckToday(new Date());
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXLoginPanel1 = new org.jdesktop.swingx.JXLoginPanel();
        labelUserName = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        textUserName = new javax.swing.JTextField();
        textPassword = new javax.swing.JPasswordField();
        buttonLogin = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        jXLoginPanel1.setBannerText("PT BERGUNA JAYA");

        labelUserName.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelUserName.setText("User Name");

        labelPassword.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelPassword.setText("Password");

        textUserName.setFont(new java.awt.Font("Tahoma", 1, 11));
        textUserName.setText("jTextField1");

        textPassword.setFont(new java.awt.Font("Tahoma", 1, 11));
        textPassword.setText("jPasswordField1");

        buttonLogin.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonLogin.setText("Login");
        buttonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLoginActionPerformed(evt);
            }
        });

        buttonCancel.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonCancel.setText("Cancel");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXLoginPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(buttonCancel)
                        .addGap(108, 108, 108))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUserName)
                            .addComponent(labelPassword))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(textPassword, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textUserName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
                        .addContainerGap(33, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXLoginPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelUserName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPassword))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(buttonLogin))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-200)/2, 400, 200);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        System.exit(0);
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void buttonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLoginActionPerformed
        try {
            if (textUserName.getText().equals("") || textPassword.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "User Kosong!!!");
            } else {
                User user = userDAO.login(textUserName.getText().toUpperCase(), textPassword.getText().toUpperCase());
                if (user.getTingkat().equals("ADMINISTRATOR")) {
                    MenuUtama menuUtama = new MenuUtama();
                    menuUtama.setVisible(true);
                    this.setVisible(false);
                }
                if (user.getTingkat().equals("PEGAWAI")) {
                    MenuUtama menuUtama = new MenuUtama();
                    menuUtama.masterBarang.setVisible(false);
                    menuUtama.masterPelanggan.setVisible(false);
                    menuUtama.masterPemasok.setVisible(false);
                    menuUtama.masterSatuan.setVisible(false);
                    menuUtama.masterUser.setVisible(false);
                    menuUtama.tukarTambahPenjualan.setVisible(false);
                    menuUtama.setVisible(true);
                    this.setVisible(false);
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "User Name Salah");
            System.exit(0);
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonLoginActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    LiquidLookAndFeel.setLiquidDecorations(true, "mac");
                    UIManager.setLookAndFeel(new LiquidLookAndFeel());
                    new Login().setVisible(true);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonLogin;
    private org.jdesktop.swingx.JXLoginPanel jXLoginPanel1;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JLabel labelUserName;
    private javax.swing.JPasswordField textPassword;
    private javax.swing.JTextField textUserName;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        textUserName.setText("");
        textPassword.setText("");
    }
}
